﻿@{
    ViewBag.PageTitle = Resources.Intercamber.StudentInvite_PageName;
}
@section PageHead
{
    <style>
        .tipWidth
        {
            max-width: 500px !important;
        }
    </style>
    <script type="text/javascript">
        var userAdder = new function() {

            var ttAdd = null;
            this.openPopupAdd = function() {
                if (ttAdd == null) {
                    ttAdd = $('<div />');
                    ttAdd.qtip({
                        show: {
                            effect: false,
                            modal: { on: true, blur: false }
                        },
                        content: $('#ttAdd'),
                        position: { my: 'center', at: 'center', target: $(window), viewport: $(window), adjust: { method: "shift", x: 0, y: 0 }, effect: false },
                        events: {
                            show: function() {
                                $(this).draggable();
                                $(this).qtip('option', { 'content.title': "@Html.Raw(Resources.Intercamber.UserAdmin_InviteUsers)", 'content.title.button': 'true' });
                            },
                            hide: function() {
                                $(this).qtip('option', { 'position.my': 'top left', 'position.target': [$(this).offset().left, $(this).offset().top] });
                            }
                        },
                        hide: false,
                        style: { classes: "qtip-bootstrap qtip-rounded tipWidth", tip: false }
                    });
                }
                $("#addContactMails").val("");
                ttAdd.qtip('show');
            };

            this.popupHide = function() {
                ttAdd.qtip('hide');
            };

            // click invite 
            this.inviteUsers = function() {
                $("#loadingInvite").show();
                this.popupHide();
                $.ajax({
                    type: "POST",
                    url: "/Admin/InviteUsers",
                    contentType: "application/json; charset=utf-8",
                    data: "{'usersToAdd':\"" + $("#addContactMails").val().replace("\"", "") + "\"}",
                    dataType: "json",
                    success: function(result) {
                        var msgMailSent = "@Html.Raw(Resources.Intercamber.UserAdmin_MsgInviteMailsSent)";
                        $("#loadingInvite").hide();
                        $("#gridInvitations").jqGrid().trigger("reloadGrid", [{ page: 1 }]);
                        alert(msgMailSent.replace("{0}", result));
                    },
                    error: function(result) {
                        $("#loadingInvite").hide();
                        debugger;
                    }
                });
            };
        };

        var gridInvitations = new function() {
            this.init = function() {
                $("#gridInvitations").jqGrid({
                    //autowidth: true,
                    datatype: 'json',
                    width: '100%',
                    height: '100%',
                    jsonReader: { 'repeatitems': false, 'id': 'IdUserInvite' },
                    loadonce: false,
                    pager: '#gridInvitations_pager',
                    rowList: [5, 10, 15],
                    rowNum: 5,
                    url: '/Admin/InvitedUsersData',
                    viewrecords: true,
                    colModel: [
                        { name: 'IdUserInvite', hidden: true },
                        { name: 'Email', label: '@Resources.Intercamber.Email', align: 'center', width: 100 },
                        { name: 'EmailSent', label: '@Resources.Intercamber.UserAdmin_EmailSent', align: 'center', width: 40, formatter: gridInvitations.sentFormater },
                        { name: 'AccountCreated', label: '@Resources.Intercamber.UserAdmin_AccountCreated', align: 'center', width: 50, formatter: gridInvitations.createdFormater },
                        { name: 'Profil', label: '@Resources.Intercamber.Profil', align: 'center', width: 30, formatter: gridInvitations.viewProfileFormater },
                        { name: 'InviteId', label: '@Resources.Intercamber.UserAdmin_CreationLink', align: 'center', formatter: gridInvitations.linkFormater },
                        { name: 'DateCreation', label: '@Resources.Intercamber.CreationDate', align: 'center', width: 60, formatter: gridInvitations.dateFormater }
                    ],
                    subGrid: false,
                    loadComplete: function() {
                        $("#gridInvitations").jqGrid('setGridWidth', 800, true);
                    },
                });
            };

            this.viewProfileFormater = function (cellvalue, options, rowobject) {
                var idUser = rowobject.IdUser;
                if (idUser == null || idUser == 0) return "";
                return "<img onclick='profilViewerManager.showProfil(" + idUser + ");' class='imgButton' src='/Images/Avatar.png' />";
            };
            
            this.dateFormater = function(cellvalue, options, rowobject) {
                var date = instanciateDotNetDate(rowobject.DateCreation);
                return date.formatDate("dd/MM/yyyy HH:mm");
            };

            this.sentFormater = function(cellvalue, options, rowobject) {
                var sent = rowobject.EmailSent;
                return sent ? "<img src=\"/Images/Check.png\" />" : "<img src=\"/Images/Delete.png\" />";
            };

            this.createdFormater = function(cellvalue, options, rowobject) {
                var created = rowobject.AccountCreated;
                return created ? "<img src=\"/Images/Check.png\" />" : "<img src=\"/Images/Delete.png\" />";
            };

            this.linkFormater = function(cellvalue, options, rowobject) {
                var guid = rowobject.InviteId;
                return "@Request.Url.ToString().Replace("Admin/StudentInvite", "")" + "Account/Create/" + guid;
            };
        };


        $(function() {
            $(".jqbutton").button();
            gridInvitations.init();
        });

    </script>
    @Html.Partial("~/Views/Shared/_ProfilViewerPartial.cshtml")
}

@section PageLeftBar {
    @Html.Partial("~/Views/Shared/_LeftBarUserDetailPartial.cshtml")
}

@section PageContent {
    <div class="innerTitle ui-corner-all">
        @Resources.Intercamber.StudentInvite_PageName
    </div>
    <br />
    <br />
    <div>
        <button id="btnAddUsers" class="jqbutton" onclick="userAdder.openPopupAdd(); return false;">@Resources.Intercamber.UserAdmin_InviteStudents</button>
        <span id="loadingInvite" style="display: none;">
            <img src="~/Images/Loading.gif" />
            @Resources.Intercamber.UserAdmin_SendingMails
        </span>
    </div>
    <br />
    <table id="gridInvitations" style="width: 100%"></table>
    <div id="gridInvitations_pager"></div>
    

    <table id='gridUsers' style="width: 100%"></table>

    <div id="ttAdd" style="display: none; width:400px;">
        @Resources.Intercamber.UserAdmin_InviteUsersDescription<br />
        @*@Resources.Intercamber.UserAdmin_InvitationsValidWarn<br/>*@
        <textarea id="addContactMails" rows="5" style='border:1px solid lightgray ; margin-top:5px;'></textarea>
        <br />
        <div class="ttButtons">
            <button class="jqbutton" onclick="userAdder.inviteUsers(); return false; ">@Resources.Intercamber.UserAdmin_InviteUsersOK</button>
            &nbsp;
            <button class="jqbutton" onclick="userAdder.popupHide(); return false;">@Resources.Intercamber.Cancel</button>
        </div>
    </div>

}
